package com.samapp.indent;

import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.dragon.image.ImageFormatConversion;
import com.samapp.MyApplication;
import com.samapp.R;
import com.samapp.view.MyPaintView;

public class PaintActivity extends Activity implements OnClickListener {

	/** 返回按钮 */
	private Button btnBack = null;
	/** 清除按钮 */
	private Button btnClear = null;
	/** 确定按钮 */
	private Button btnSure = null;
	private MyPaintView myPaintView = null;
	private MyApplication myApplication = MyApplication.getApplicationInstance();
	/** 签名结果码 */
	public static final int CODE_AUTOGRAPH = 0xa3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_paint);
		init();
	}

	private void init() {
		initView();
		initListener();
	}

	private void initView() {
		btnBack = (Button) findViewById(R.id.btn_back);
		btnClear = (Button) findViewById(R.id.btn_clear);
		btnSure = (Button) findViewById(R.id.btn_sure);
		myPaintView = (MyPaintView) findViewById(R.id.view_paint);
	}

	private void initListener() {
		btnBack.setOnClickListener(this);
		btnClear.setOnClickListener(this);
		btnSure.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_back:
			finish();
			break;
		case R.id.btn_clear:
			myPaintView.clear();
			break;
		case R.id.btn_sure:
			Intent intent = new Intent();
			myApplication.bitmapAutograph = myPaintView.getMyBitmap();
			try {
				ImageFormatConversion.bitmapToFile(myPaintView.getMyBitmap(), "autograph.JPEG", "/mnt/sdcard/samapp/");
			} catch (IOException e) {
				e.printStackTrace();
			}
			setResult(CODE_AUTOGRAPH, intent);
			finish();
		}
	}
}
